(function() {
  app.controller('vipCancelCtrl', function(alertTip, $timeout, $scope, local_m, vipIndex, $state, bottomdilog, tradeApi, centerdilog) {
    var card_ordertype_arr = [
      '限价委托',
      '对手方最优价格委托',
      '五档即时成交剩余转限',
      '本方最优价格委托',
      '即时成交剩余撤销委托',
      '五档即时成交剩余撤销',
      '全额成交或撤销委托',
    ];
    $scope.init = function() {
      tradeApi
        .etQuery({
          funcid: '2',
          showtype: '2'
        })
        .then(function(res) {
          angular.forEach(res, function(item) {
            item.card_ordertype = card_ordertype_arr[item.ordertype];
            item.card_state = '委托中';
          });
          $scope.dir_cards.data = res;
        });
    };

    $scope.dir_cards = {
      data: [],
      config: {
        title: {
          css: {},
          content: { 'key1': 'stkcode', 'key2': 'stkname', 'split': '--' },
        },
        rows: {
          css: {
            left: {},
            right: {}
          },
          content: [
            { txt: '委托价格', key: 'price', unit: '元 / 股' },
            { txt: '委托数量', key: 'amt', unit: '股' },
            { txt: '交易方向', key: 'bstype' },
            { txt: '申报方式', key: 'card_ordertype' },
            { txt: '成交数量', key: 'matchamt', unit: '股' },
            { txt: '成交金额', key: 'matchbalance', unit: '元' },
            { txt: '委托时间', key: 'date', key2: 'time' },
          ],
        },
        buttons: {
          cancel_txt: '撤单',
          cancel_show: true,
          ok_show: false,
          cancel: function(obj) {
            $scope.cancel(obj);
          }
        },
        state: {
          show: true,
          key: 'card_state'
        }
      },
    };

    $scope.cancel = function(item) {
      centerdilog
        .open({
          msg: '确定要撤销该笔委托吗？',
          ok: function() {
            $scope.query_cancel(item.obj.sno);
          }
        });

    };

    $scope.query_cancel = function(no) {
      tradeApi
        .etQuery({
          funcid: '11',
          orderno: no
        })
        .then(function(res) {
          alertTip
            .open({
              msg: '撤单委托提交成功！',
              state: 1,
            });
          $timeout(function() {
            $scope.init();
          }, 2000);
        });
    };
  });

})();